Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

144
Vistas
target "this" on addEventListener event

So I'm dealing with a little problem..

I'm trying to modify drag-and-drop code and came across a problem, since I want to have multiple drop areas and one function for all of them.

I have this code:

dropArea.addEventListener("dragover", (event)=>{
  event.preventDefault(); //preventing from default behaviour
  dropArea.classList.add("activated");
  dragText.textContent = "🖐 Release to Upload File";
});

And specified dropareawitth querySelector:

const dropArea = document.querySelector(".drag-area")

Now I need to modify it to work with multiple drop zones. I thought to just replace the "droparea" inside the event to "this" something like that:

this.classList.add("activated");

but it does not work. I even tried:

$(this).classList.add("activated");

Returns this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'add')

Any solutions? I'm stuck...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As your event handler is an arrow function, the value of this is not influenced by the caller, so you cannot use it to get the DOM element. You can use event.target.

event.target.classList.add("activated");

As to your final attempt: $() does not return a DOM element, but an array-like "jQuery" collection object, which doesn't have such properties as classList.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda